<?php
require_once("EKRating.php");

class EKEvaluation {
	static $documentV = 100;
	static $paragraphV = 25;
	static $talkV = 5;
	static $preliminaryV = 500;
	static $expFactor = 4;
	static $powerFactor = 1.6;
	
	# Return lv from exp
	static function calculateLv($exp){
		$u = (1+1/EKEvaluation::$expFactor);
		return floor(log((1-(1-$u)*$exp/(EKEvaluation::$preliminaryV)),$u));
	}
	
	# RatingWeight Transformation
	static function transformRW($w){
		if($w <= 1){
			$y = 0;
		} else if (1 < $w && $w <= 10){
			$y = ($w-1)*0.01/9;
		} else if (10 < $w && $w <= 100){
			$y = ($w-10)*0.1/90;
		} else if (100 < $w && $w <= 1000){
			$y = ($w-100)*0.5/900;
		} else if (1000 < $w && $w <= 10000){
			$y = ($w-1000)*0.8/9000;
		} else if (10000 < $w && $w <= 100000){
			$y = ($w-10000)*1/90000;
		} else if ($w > 100000){
			$y = 1;
		} else {
			$y = 0;
		}
		return $y;
	}
	
	# Return finalExp from OriginalExp
	static function transformExp($exp,$ratingAValue,$ratingAWeight,$ratingBValue,$ratingBWeight){
		return $exp*0.5* 
				(2 +
				EKEvaluation::transformRW($ratingAWeight)*(pow(EKEvaluation::$powerFactor,$ratingAValue - 3)-1) + 
				EKEvaluation::transformRW($ratingAWeight)*(pow(EKEvaluation::$powerFactor,$ratingBValue - 3)-1)
				);
	}
	
	#  Get ratingWeight from user lv
	static function lvToRatingWeight($lv){
		return sqrt($lv + 1);
	}
	
	# Evaluation Table
	static function getEvaluation($eventType,$elementType){
		$rule = array( 	"d" => array(# doucment
								"create" => 20,
								"delete" => -20,
								"approve" => EKEvaluation::$documentV,
								"disapprove" => -EKEvaluation::$documentV,
								"deny" => -EKEvaluation::$documentV
								),
						"p" => array(# paragraph
								"create" => 10,
								"delete" => -10,
								"modify" => 0,
								"approve" => EKEvaluation::$paragraphV,
								"disapprove" => -EKEvaluation::$paragraphV,
								"deny" => -EKEvaluation::$paragraphV
								),
						"t" => array(# talk
								"create" => EKEvaluation::$talkV,
								"deny" => -2 * EKEvaluation::$talkV
								),
						"r" => array(# rating
								"create" => 1
								)
						);
		return $rule[$elementType][$eventType];
	}
}
?>